from django.db.models import Q
from django.http import JsonResponse
from utils.views import LoginRequiredJsonMixin
from django.shortcuts import render

# Create your views here.
from django.views import View
from apps.products.models import Product
from apps.Merchants.models import Merchant
from django.core.paginator import Paginator

# Product.objects.create(
#     product_name='牛肉堡',
#     description='风味正宗，仿佛置身美国乡村牛排馆',
#     img_url='images/2021051902.jpg',
#     price=17.50,
#     type_id=1,
#     num=100,
#     merchant_id=7,
#     sales=88
# )


class ListView(View):
    """商品数据展示"""

    def get(self, request):
        # product = Product.objects.get(product_id=1)
        # print(product.merchant.merchant_name)
        # 接收get请求中的参数
        ordering = request.GET.get('ordering')
        page_size = request.GET.get('page_size')
        page = request.GET.get('page')
        # 将数据库中存放的商品信息按照传递过来的ordering进行排序
        products_obj = Product.objects.filter(is_delete=False).order_by(ordering)
        # 实现分页: 参数1：要实现分页的数据；参数2：每页多少条数据
        paginator = Paginator(products_obj, per_page=page_size)
        page_products = paginator.page(page)  # 获取指定页的数据
        product_list = []
        for products in page_products.object_list:
            product_list.append({
                'id': products.product_id,
                'name': products.product_name,
                'image_url': products.img_url,
                'price': products.price,
                'sales': products.sales,
                'descript': products.description,
                'source': Product.objects.get(product_id=products.product_id).merchant.merchant_name
            })
        # 获取商品数据的总页数
        total_num = paginator.num_pages
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'list': product_list,
            'count': total_num
        })


class SearchView(View):
    """搜索商品"""

    def get(self, request):
        ordering = request.GET.get('ordering')
        page_size = request.GET.get('page_size')
        page = request.GET.get('page')
        q = request.GET.get('q')
        products = Product.objects.filter(Q(product_name__contains=q) | Q(merchant__merchant_name__contains=q) | Q(type__type__contains=q) & Q(is_delete=False)).order_by(
            ordering)
        paginator = Paginator(products, per_page=page_size)
        page_product = paginator.page(page)
        product_list = []
        for products in page_product.object_list:
            product_list.append({
                'id': products.product_id,
                'name': products.product_name,
                'image_url': products.img_url,
                'price': products.price,
                'sales': products.sales,
                'descript': products.description,
                'source': Product.objects.get(product_id=products.product_id).merchant.merchant_name
            })
        # 获取商品数据的总页数
        total_num = paginator.num_pages
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'list': product_list,
            'count': total_num
        })


class DetailView(LoginRequiredJsonMixin, View):
    """获取商品的详细信息"""

    def get(self, request, id):
        try:
            product = Product.objects.get(product_id=id, is_delete=False)
        except Product.DoesNotExist:
            return JsonResponse({'code': 401, 'errmsg': '该商品id不存在'})
        return JsonResponse({
            'code': 0,
            'errmsg': 'ok',
            'id': product.product_id,
            'name': product.product_name,
            'image_url': product.img_url,
            'price': product.price,
            'sales': product.sales,
            'descript': product.description,
            'num': product.num,
            'source': product.merchant.merchant_name
        })
        # products = Product.objects.filter(product_id=id)
        # product_list = []
        # for product in products:
        #     product_list.append({
        #         'id': product.product_id,
        #         'name': product.product_name,
        #         'image_url': product.img_url,
        #         'price': product.price,
        #         'sales': product.sales,
        #         'descript': product.description,
        #         'num': product.num
        #     })
        # return JsonResponse({
        #     'code': 0,
        #     'errmsg': 'ok',
        #     'product_list': product_list
        # })
